import { Component, OnInit } from '@angular/core';
import { InteractiveService } from 'src/app/index/interactive.service';
import { LoadService } from 'src/app/base/gis/load.service';

declare var map: any;
declare var form: any;
declare var axios: any;

@Component({
  selector: 'app-gis2',
  templateUrl: './gis2.component.html',
  styleUrls: ['./gis2.component.scss']
})
export class Gis2Component implements OnInit {

  p: any;
  es: any;
  ms: any;

  // 以下为gis交互管理
  public mIsCollapsed = false;

  constructor(public is: InteractiveService) {
    // 自带查询初始化
    axios.AxiosService.init();

    // 初始化地图
    this.p = new map.MapService('EPSG:4326', 'EPSG:3857', true,
      { xmin: 105.992204, ymin: 36.979711, xmax: 109.5338915, ymax: 39.543712 },
      'mapboxgl');
    form.ConfigService.map = this.p;
  }

  ngOnInit() {
    // 初始化图层加载对象
    form.ConfigService.ls = new LoadService(this.p);

    // 初始化完成事件
    this.p.on('MapInit', () => {
      // 打印服务
      this.es = new map.ExportService(this.p);
      // 测量服务
      this.ms = new map.MeasureService(this.p);
    });
    // 初始化地图控件
    this.p.initMap('map', 'cesium', {
      projection: 'EPSG:4326',
      center: [107.617, 38.359],
      zoom: 9,
      maxZoom: 20,
      minZoom: 7,
      attributionControl: false, // 关闭属性框
      crossSourceCollisions: false, // 关闭不同数据源之间的冲突检测
      style: 'mapbox://styles/itcz/ck2c3wksk04zn1clc3nyx99uy',
    });
    // 注册样式切换事件
    this.p.on('style.load', (e) => {
      if (this.p.mapInited === true && form.ConfigService.styleChange) {
        form.ConfigService.styleChange();
      }
    });
  }

  /**
   * 设置是否关闭地图则栏
   */
  mapMargin() {
    this.mIsCollapsed = !this.mIsCollapsed;
  }

  // 是否为2D地图
  is2DMap() {
    return form.ConfigService.map.is2DMap();
  }
}
